package com.example.yls.demoa;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
private Button btn2;
private Button btn3;
private Button btn1;
private Button btn4;
    private static final String TAG = "MainActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initBundler();
        btn2Event();
        btn3Event();
        btn1Event();
        btn4Envent();
    }

    private void btn4Envent() {
               btn4.setOnClickListener(new View.OnClickListener() {
                   @Override
                   public void onClick(View view) {
                       try {
                           InputStream inputStream=getResources().getAssets().open("data.txt");
                           BufferedReader bufferedReader=new
                                   BufferedReader(new InputStreamReader(inputStream));
                           StringBuffer stringBuffer=new StringBuffer();

                           String line=bufferedReader.readLine();
                           while (line!=null){
                               stringBuffer.append(line);
                               line=bufferedReader.readLine();
                           }
                       /*    Log.e(TAG, "StringBuffer"+stringBuffer );*/
                     JSONArray jsonArray=new JSONArray(stringBuffer.toString());

                     for(int i=0;i<jsonArray.length();i++){
                         JSONObject jsonObject=  jsonArray.getJSONObject(i);
                         if (jsonObject.getString("name").equals("广东省")){
                           JSONArray cityArray=  jsonObject.getJSONArray("city");

                           for (int j=0;j<cityArray.length();j++){

                               JSONObject cityJson= cityArray.getJSONObject(j);
                               if (cityJson.getString("name").equals("广州市")){
                                   JSONArray quArr=cityJson.getJSONArray("area");

                                   for (int k=0;k<quArr.length();k++){
                                      String areaName=quArr.getString(k);

                                       Log.e(TAG, "area："+areaName );

                                   }




                               }

                           }




                         }


                     }







                       } catch (IOException e) {
                           e.printStackTrace();
                       } catch (JSONException e) {
                           e.printStackTrace();
                       }
                   }
               });
    }


    private void btn1Event() {
             btn1.setOnClickListener(new View.OnClickListener() {
                 @Override
                 public void onClick(View view) {
                     String s="[\n" +
                             "    { \"id\":\"1\",\"name\":\"张三\",\"age\":\"18\" },\n" +
                             "    { \"id\":\"2\",\"name\":\"李四\",\"age\":\"18\"  },\n" +
                             "    { \"id\":\"3\",\"name\":\"王五\",\"age\":\"18\" }\n" +
                             "]";
                     try {
                         JSONArray jsonArray=new JSONArray(s);

                         for (int i=0;i<jsonArray.length();i++){
                           JSONObject jsonObject= (JSONObject) jsonArray.get(i);
                             Log.e(TAG, "jsonObject"+jsonObject.toString() );
                         }

                     } catch (JSONException e) {
                         e.printStackTrace();
                     }


                 }


             });
    }

    private void btn3Event() {
          btn3.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {

                  ArrayList<Student> studentArrayList=new ArrayList<>();

                  Student s1=new Student("zhangsan","171001",18);
                  Student s2=new Student("lisi","171002",12);
                  Student s3=new Student("wangwu","171003",10);

                  studentArrayList.add(s1);
                  studentArrayList.add(s2);
                  studentArrayList.add(s3);


                  JSONArray jsonArray=new JSONArray();

                  for (Student s:studentArrayList){

                      try {
                          JSONObject jsonObject=new JSONObject();
                          jsonObject.put("name",s.getName());
                          jsonObject.put("age",s.getAge());
                          jsonObject.put("stuno",s.getStuNo());
                          jsonArray.put(jsonObject);
                      } catch (JSONException e) {
                          e.printStackTrace();
                      }
                  }

                  JSONObject jsonObject=new JSONObject();

                  try {
                      jsonObject.put("StuList",jsonArray);
                  } catch (JSONException e) {
                      e.printStackTrace();
                  }

                  Log.e(TAG, "JsonObject"+jsonObject.toString() );


              }
          });
    }

    private void btn2Event() {
             btn2.setOnClickListener(new View.OnClickListener() {
                 @Override
                 public void onClick(View view) {
                     Student s1=new Student("zhangsan","171001",18);

                     JSONObject jsonObject=new JSONObject();
                     try {
                         jsonObject.put("name",s1.getName());
                         jsonObject.put("age",s1.getAge());
                         jsonObject.put("stuno",s1.getStuNo());
                         Log.e(TAG, "JsonObject"+jsonObject.toString() );
                     } catch (JSONException e) {
                         e.printStackTrace();
                     }






                 }
             });
    }

    private void initBundler() {
            btn2=findViewById(R.id.btn2);
            btn3=findViewById(R.id.btn3);
            btn1=findViewById(R.id.btn1);
            btn4=findViewById(R.id.btn4);
    }
}
